package com.inspsys.inspection.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.inspsys.common.annotation.Log;
import com.inspsys.common.core.controller.BaseController;
import com.inspsys.common.core.domain.AjaxResult;
import com.inspsys.common.enums.BusinessType;
import com.inspsys.inspection.domain.InspectionTask;
import com.inspsys.inspection.service.IInspectionTaskService;
import com.inspsys.common.utils.poi.ExcelUtil;
import com.inspsys.common.core.page.TableDataInfo;

/**
 * 巡检任务Controller
 *
 * @author inspsys
 * @date 2025-01-08
 */
@RestController
@RequestMapping("/inspection/task")
public class InspectionTaskController extends BaseController {
    @Autowired
    private IInspectionTaskService inspectionTaskService;

    /**
     * 查询巡检任务列表
     */
    @PreAuthorize("@ss.hasPermi('inspection:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(InspectionTask inspectionTask) {
        startPage();
        List<InspectionTask> list = inspectionTaskService.selectInspectionTaskList(inspectionTask);
        return getDataTable(list);
    }

    /**
     * 导出巡检任务列表
     */
    @PreAuthorize("@ss.hasPermi('inspection:task:export')")
    @Log(title = "巡检任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InspectionTask inspectionTask) {
        List<InspectionTask> list = inspectionTaskService.selectInspectionTaskList(inspectionTask);
        ExcelUtil<InspectionTask> util = new ExcelUtil<InspectionTask>(InspectionTask.class);
        util.exportExcel(response, list, "巡检任务数据");
    }

    /**
     * 获取巡检任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('inspection:task:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(inspectionTaskService.selectInspectionTaskById(id));
    }

    /**
     * 新增巡检任务
     */
    @PreAuthorize("@ss.hasPermi('inspection:task:add')")
    @Log(title = "巡检任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InspectionTask inspectionTask) {
        return toAjax(inspectionTaskService.insertInspectionTask(inspectionTask));
    }

    /**
     * 修改巡检任务
     */
    @PreAuthorize("@ss.hasPermi('inspection:task:edit')")
    @Log(title = "巡检任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InspectionTask inspectionTask) {
        return toAjax(inspectionTaskService.updateInspectionTask(inspectionTask));
    }

    /**
     * 删除巡检任务
     */
    @PreAuthorize("@ss.hasPermi('inspection:task:remove')")
    @Log(title = "巡检任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(inspectionTaskService.deleteInspectionTaskByIds(ids));
    }

    /**
     * 查询巡检缺陷结果
     */
    @PreAuthorize("@ss.hasPermi('inspection:task:query')")
    @PostMapping("/getDetectResults")
    public AjaxResult queryDetectResult(@RequestBody InspectionTask inspectionTask) {
        return AjaxResult.success(inspectionTaskService.queryDetectResult(inspectionTask.getOrderNumber()));
    }

    /**
     * 导出巡检任务
     * 工单报告
     * 
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('inspection:task:export')")
    @Log(title = "巡检任务", businessType = BusinessType.EXPORT)
    @PostMapping("/exportReport")
    public void exportReport(HttpServletResponse response, InspectionTask inspectionTask) throws Exception {
        String dest = "report_" + inspectionTask.getOrderNumber() + ".pdf";
        byte[] outByte = inspectionTaskService.exportReport(inspectionTask);
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.setHeader("Content-Disposition", "attachment; filename=\"" + dest + "\"");
        response.setContentLength(outByte.length);
        try {
            response.getOutputStream().write(outByte);
            response.getOutputStream().flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
